package com.opensourceedge.phonebook.cli;

import java.util.Scanner;

import com.opensourceedge.phonebook.api.Contact;

public class Util {

	/*
	 * takes input from CLI and assigns them to Contact respective properties
	 * passed as a parameter.
	 */
	@SuppressWarnings("resource")
	public static void addContactCLI(Contact con) {
		Scanner sn = null;

		while (true) {
			// asks for input and will also display current value if available.
			System.out.print("First Name");
			if (con.getFirstName() != null) {
				System.out.print("[" + con.getFirstName() + "]");
			}
			System.out.print(" :");

			// gets input from command.
			sn = new Scanner(System.in);
			sn.useDelimiter("\n");

			try {
				String temp = sn.next();
				// trims temp to remove \n from user input.
				String input = temp.substring(0, temp.length() - 1);
				// System.out.println("input is>"+input+"<length="+input.length());
				if (input.length() == 0) {
					// System.out.println("you entered ENTER.");
				} else {
					con.setFirstName(input);
				}
				break;
			} catch (Exception e) {
				System.out
						.println("<<Error>> Invalid First Name !!try again!!");
			}
		}

		while (true) {
			// asks for input and will also display current value if available.
			System.out.print("Last Name");
			if (con.getLastName() != null) {
				System.out.print("[" + con.getLastName() + "]");
			}
			System.out.print(" :");

			// gets input from command.
			sn = new Scanner(System.in);
			sn.useDelimiter("\n");

			try {
				String temp = sn.next();
				// trims temp to remove \n from user input.
				String input = temp.substring(0, temp.length() - 1);
				// System.out.println("input is>"+input+"<length="+input.length());
				if (input.length() == 0) {
					// System.out.println("you entered ENTER.");
				} else {
					con.setLastName(input);
				}
				break;
			} catch (Exception e) {
				System.out.println("<<Error>> Invalid Last Name !!try again!!");
			}
		}

		while (true) {
			// asks for input and will also display current value if available.
			System.out.print("Mobile Phone");
			if (con.getMobilePhone() != null) {
				System.out.print("[" + con.getMobilePhone() + "]");
			}
			System.out.print(" :");

			// gets input from command.
			sn = new Scanner(System.in);
			sn.useDelimiter("\n");

			try {
				String temp = sn.next();
				// trims temp to remove \n from user input.
				String input = temp.substring(0, temp.length() - 1);
				// System.out.println("input is>"+input+"<length="+input.length());
				if (input.length() == 0) {
					// System.out.println("you entered ENTER.");
				} else {
					con.setMobilePhone(input);
				}
				break;
			} catch (Exception e) {
				System.out
						.println("<<Error>> Invalid Mobile Phone !!try again!!");
			}
		}

		while (true) {
			// asks for input and will also display current value if available.
			System.out.print("Work Phone");
			if (con.getWorkPhone() != null) {
				System.out.print("[" + con.getWorkPhone() + "]");
			}
			System.out.print(" :");

			// gets input from command.
			sn = new Scanner(System.in);
			sn.useDelimiter("\n");

			try {
				String temp = sn.next();
				// trims temp to remove \n from user input.
				String input = temp.substring(0, temp.length() - 1);
				// System.out.println("input is>"+input+"<length="+input.length());
				if (input.length() == 0) {
					// System.out.println("you entered ENTER.");
				} else {
					con.setWorkPhone(input);
				}
				break;
			} catch (Exception e) {
				System.out
						.println("<<Error>> Invalid Work Phone !!try again!!");
			}
		}

		while (true) {
			// asks for input and will also display current value if available.
			System.out.print("Company");
			if (con.getCompanyName() != null) {
				System.out.print("[" + con.getCompanyName() + "]");
			}
			System.out.print(" :");

			// gets input from command.
			sn = new Scanner(System.in);
			sn.useDelimiter("\n");

			try {
				String temp = sn.next();
				// trims temp to remove \n from user input.
				String input = temp.substring(0, temp.length() - 1);
				// System.out.println("input is>"+input+"<length="+input.length());
				if (input.length() == 0) {
					// System.out.println("you entered ENTER.");
				} else {
					con.setCompanyName(input);
				}
				break;
			} catch (Exception e) {
				System.out
						.println("<<Error>> Invalid Company Name !!try again!!");
			}
		}

		while (true) {
			// asks for input and will also display current value if available.
			System.out.print("Street");
			if (con.getStreetName() != null) {
				System.out.print("[" + con.getStreetName() + "]");
			}
			System.out.print(" :");

			// gets input from command.
			sn = new Scanner(System.in);
			sn.useDelimiter("\n");

			try {
				String temp = sn.next();
				// trims temp to remove \n from user input.
				String input = temp.substring(0, temp.length() - 1);
				// System.out.println("input is>"+input+"<length="+input.length());
				if (input.length() == 0) {
					// System.out.println("you entered ENTER.");
				} else {
					con.setStreetName(input);
				}
				break;
			} catch (Exception e) {
				System.out
						.println("<<Error>> Invalid Street Name !!try again!!");
			}
		}

		while (true) {
			// asks for input and will also display current value if available.
			System.out.print("Building Number");
			if (con.getBuildingNumber() != null) {
				System.out.print("[" + con.getBuildingNumber() + "]");
			}
			System.out.print(" :");

			// gets input from command.
			sn = new Scanner(System.in);
			sn.useDelimiter("\n");

			try {
				String temp = sn.next();
				// trims temp to remove \n from user input.
				String input = temp.substring(0, temp.length() - 1);
				// System.out.println("input is>"+input+"<length="+input.length());
				if (input.length() == 0) {
					// System.out.println("you entered ENTER.");
				} else {
					con.setBuildingNumber(input);
				}
				break;
			} catch (Exception e) {
				System.out
						.println("<<Error>> Invalid Building Number !!try again!!");
			}
		}

		while (true) {
			// asks for input and will also display current value if available.
			System.out.print("City");
			if (con.getCity() != null) {
				System.out.print("[" + con.getCity() + "]");
			}
			System.out.print(" :");

			// gets input from command.
			sn = new Scanner(System.in);
			sn.useDelimiter("\n");

			try {
				String temp = sn.next();
				// trims temp to remove \n from user input.
				String input = temp.substring(0, temp.length() - 1);
				// System.out.println("input is>"+input+"<length="+input.length());
				if (input.length() == 0) {
					// System.out.println("you entered ENTER.");
				} else {
					con.setCity(input);
				}
				break;
			} catch (Exception e) {
				System.out.println("<<Error>> Invalid City !!try again!!");
			}
		}

		while (true) {
			// asks for input and will also display current value if available.
			System.out.print("State");
			if (con.getState() != null) {
				System.out.print("[" + con.getState() + "]");
			}
			System.out.print(" :");

			// gets input from command.
			sn = new Scanner(System.in);
			sn.useDelimiter("\n");

			try {
				String temp = sn.next();
				// trims temp to remove \n from user input.
				String input = temp.substring(0, temp.length() - 1);
				// System.out.println("input is>"+input+"<length="+input.length());
				if (input.length() == 0) {
					//System.out.println("you entered ENTER.");
				} else {
					con.setState(input);
				}
				break;
			} catch (Exception e) {
				System.out.println("<<Error>> Invalid State !!try again!!");
			}
		}

		while (true) {
			// asks for input and will also display current value if available.
			System.out.print("Zip Code");
			if (con.getZipCode() != null) {
				System.out.print("[" + con.getZipCode() + "]");
			}
			System.out.print(" :");

			// gets input from command.
			sn = new Scanner(System.in);
			sn.useDelimiter("\n");

			try {
				String temp = sn.next();
				// trims temp to remove \n from user input.
				String input = temp.substring(0, temp.length() - 1);
				// System.out.println("input is>"+input+"<length="+input.length());
				if (input.length() == 0) {
					// System.out.println("you entered ENTER.");
				} else {
					con.setZipCode(input);
				}
				break;
			} catch (Exception e) {
				System.out.println("<<Error>> Zip Code !!try again!!");
			}
		}
	}
}
